package com.masterclient.app.modules.homepage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.eoner.baselibrary.bean.firstpager.RecommendPagerBean;
import com.eoner.baselib.utils.notify.RxBus;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.firstpager.fragments.interfaces.ItemClickListener;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RemAdapter extends RecyclerView.Adapter {


    private Context context;

    private List<RecommendPagerBean.ShTemporary> shTemporaries;

    RxBus rxBus;


    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public RemAdapter(Context context, List<RecommendPagerBean.ShTemporary> shTemporaries, RxBus rxBus) {
        this.context = context;
        this.shTemporaries = shTemporaries;
        this.rxBus = rxBus;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rem_item,parent,false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,  int position) {

        ViewHolder viewHolder = (ViewHolder) holder;

        RecommendPagerBean.ShTemporary shTemporary = shTemporaries.get(position);

//        App.picasso.load(shTemporary.getSh_image()).
//                //resize(ScreenUtils.dp2px(60),ScreenUtils.dp2px(60)).
//                        placeholder(R.mipmap.defaults_1).into(viewHolder.sh_temporary_img);
        //
        int width = ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(30);
        int height = (int) (width / 686f * 170);
        GlideUtil.displayRoundImage(context,shTemporary.getSh_image(),viewHolder.sh_temporary_img,1,R.mipmap.defaults_1,width,height);

        if(position == shTemporaries.size() - 1){
            viewHolder.divider.setVisibility(View.GONE);
        }else{
            viewHolder.divider.setVisibility(View.VISIBLE);
        }

        viewHolder.sh_temporary_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onItemClick(position);
            }
        });

        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


    }

    @Override
    public int getItemCount() {
        return shTemporaries.size();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
        @BindView(R.id.sh_temporary_img)
        public ImageView sh_temporary_img;
        @BindView(R.id.divider)
        public View divider;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }


}

